//목록리스트 2단계 액티비티
package kr.jin.zeroprojectone;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class DBListAcitiviryStep2 extends Activity {
	ArrayList <MyItemsDBList2> arList2 = new ArrayList<MyItemsDBList2>();
	ArrayList <MyItemsDBList2> arList3 = new ArrayList<MyItemsDBList2>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dblist_activity_step2);
		setAdapterAndList2();	
	}
	
	private void setAdapterAndList2() {
		//db목록 리스트 -STEP2
		DBHelper dbHelper= new DBHelper(this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor c1 = db.rawQuery("SELECT * FROM step2", null);	
		Cursor c2 = db.rawQuery("SELECT * FROM step3", null);
		c1.moveToFirst();	
		c1.getCount();
		c2.moveToFirst();
		c2.getCount();

		MyItemsDBList2 dbItem2;
		MyItemsDBList2 dbItem3; 
		String text1 = getResources().getString(R.string.class_dblistactivity_timetext1);
		String text2 = getResources().getString(R.string.class_dblistactivity_timetext2);
		String text3 = getResources().getString(R.string.class_dblistactivity_timetext3);
				
		for(int i = 0; i<c1.getCount(); i++){	
			int id = c1.getInt(0);
			String date = c1.getString(1);
			String activity = c1.getString(2);
			Long time = c1.getLong(3);
			
			String str = String.format("%02d"+text1+"%02d"+text2+"%02d"+text3, time/1000/60/60, (time/1000/60)%60, (time/1000)%60);
			dbItem2 = new MyItemsDBList2(id, date, activity, str);	
			arList2.add(dbItem2);
			c1.moveToNext();			
		}
		
		for(int i = 0; i<c2.getCount(); i++){	
			int id = c2.getInt(0);
			String date = c2.getString(1);
			String activity = c2.getString(2);
			Long time = c2.getLong(3);
			String str = String.format("%02d"+text1+"%02d"+text2+"%02d"+text3, time/1000/60/60, (time/1000/60)%60, (time/1000)%60);
			dbItem3 = new MyItemsDBList2(id, date, activity, str);	
			arList3.add(dbItem3);
			c2.moveToNext();			
		}
		
		DBListAdapterStep2 dblist2 = new DBListAdapterStep2(this, R.layout.dbcustomlistview2, arList2);
		ListView DBListStep2 = (ListView)findViewById(R.id.dblist_activity_step2_main);
		DBListStep2.setAdapter(dblist2);
		DBListStep2.setOnItemClickListener(mItemListener2);
		
		DBListAdapterStep3 dblist3 = new DBListAdapterStep3(this, R.layout.dbcustomlistview3, arList3);
		ListView DBListStep3 = (ListView)findViewById(R.id.dblist_activity_step2_sub);
		DBListStep3.setAdapter(dblist3);
		DBListStep3.setOnItemClickListener(mItemListener3);
		
		dbHelper.close();
	}
	
	//첫번째 리스트뷰 
	private void setAdapterAndList22() {
		
		DBHelper dbHelper= new DBHelper(this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor c1 = db.rawQuery("SELECT * FROM step2", null);	
		c1.moveToFirst();	
		c1.getCount();
		
		MyItemsDBList2 dbItem2;
	
		for(int i = 0; i<c1.getCount(); i++){	
			int id = c1.getInt(0);
			String date = c1.getString(1);
			String activity = c1.getString(2);
			Long time = c1.getLong(3);
			String text1 = getResources().getString(R.string.class_dblistactivity_timetext1);
			String text2 = getResources().getString(R.string.class_dblistactivity_timetext2);
			String text3 = getResources().getString(R.string.class_dblistactivity_timetext3);
			String str = String.format("%02d"+text1+"%02d"+text2+"%02d"+text3, time/1000/60/60, (time/1000/60)%60, (time/1000)%60);
			dbItem2 = new MyItemsDBList2(id, date, activity, str);	
			arList2.add(dbItem2);
			c1.moveToNext();			
		}
		
		
		DBListAdapterStep2 dblist2 = new DBListAdapterStep2(this, R.layout.dbcustomlistview2, arList2);
		ListView DBListStep2 = (ListView)findViewById(R.id.dblist_activity_step2_main);
		DBListStep2.setAdapter(dblist2);
		DBListStep2.setOnItemClickListener(mItemListener2);
		
		dbHelper.close();
	}
	
	//두번째 리스트뷰
	private void setAdapterAndList23() {
		//db목록 리스트 -STEP2
		DBHelper dbHelper= new DBHelper(this);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor c2 = db.rawQuery("SELECT * FROM step3", null);
		c2.moveToFirst();
		c2.getCount();

		MyItemsDBList2 dbItem3; 
		
		for(int i = 0; i<c2.getCount(); i++){	
			int id = c2.getInt(0);
			String date = c2.getString(1);
			String activity = c2.getString(2);
			Long time = c2.getLong(3);
			String text1 = getResources().getString(R.string.class_dblistactivity_timetext1);
			String text2 = getResources().getString(R.string.class_dblistactivity_timetext2);
			String text3 = getResources().getString(R.string.class_dblistactivity_timetext3);
			String str = String.format("%02d"+text1+"%02d"+text2+"%02d"+text3, time/1000/60/60, (time/1000/60)%60, (time/1000)%60);
			dbItem3 = new MyItemsDBList2(id, date, activity, str);	
			arList3.add(dbItem3);
			c2.moveToNext();			
		}
		
		DBListAdapterStep3 dblist3 = new DBListAdapterStep3(this, R.layout.dbcustomlistview3, arList3);
		ListView DBListStep3 = (ListView)findViewById(R.id.dblist_activity_step2_sub);
		DBListStep3.setAdapter(dblist3);
		DBListStep3.setOnItemClickListener(mItemListener3);
		
		dbHelper.close();
	}
	
	AdapterView.OnItemClickListener mItemListener2 = new AdapterView.OnItemClickListener() {
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			
			showDeleteDalog(2,position);
			/*DBHelper dbHelper= new DBHelper(DBListAcitiviryStep2.this);
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			
			int i = arList2.get(position).id2;
			
			//int i = (Integer) parent.getAdapter().getItem(position);
			
			db.execSQL("DELETE FROM step2 WHERE _id = "+ i + ";");
		
			arList2.clear();
			arList3.clear();
			dbHelper.close();
			setAdapterAndList2();*/
			
		}
	};
	AdapterView.OnItemClickListener mItemListener3 = new AdapterView.OnItemClickListener() {
		
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			
			showDeleteDalog(3,position);
			/*DBHelper dbHelper= new DBHelper(DBListAcitiviryStep2.this);
			SQLiteDatabase db = dbHelper.getWritableDatabase();
	
			int j = arList3.get(position).id2;
			//int i = (Integer) parent.getAdapter().getItem(position);
	
			db.execSQL("DELETE FROM step3 WHERE _id = "+ j + ";");
			arList2.clear();
			arList3.clear();
			dbHelper.close();
			setAdapterAndList2();	*/	
		}
	};
	
	void showDeleteDalog(int num, int position){
		switch(num){
		case 2:
			FragmentManager fm2 = getFragmentManager();
			FragmentTransaction tr2 = fm2.beginTransaction();
			Fragment fg2 = fm2.findFragmentByTag("delete2");
			if(fg2!=null){
				tr2.remove(fg2);
			}
			FragmentDialogDeleteList fd2 = FragmentDialogDeleteList.newInstance(num, position);
			fd2.show(tr2, "delete2");
			break;
		case 3:
			FragmentManager fm3 = getFragmentManager();
			FragmentTransaction tr3 = fm3.beginTransaction();
			Fragment fg3 = fm3.findFragmentByTag("delete3");
			if(fg3!=null){
				tr3.remove(fg3);
			}
			FragmentDialogDeleteList fd3 = FragmentDialogDeleteList.newInstance(num, position);
			fd3.show(tr3, "delete3");
			break;
		}
		
	}
	
	void DeleteListPositiveDialog(int num, int position){
		switch(num){
		case 2:
			DBHelper dbHelper2= new DBHelper(DBListAcitiviryStep2.this);
			SQLiteDatabase db2 = dbHelper2.getWritableDatabase();
			
			int i2 = arList2.get(position).id2;
			//int i = (Integer) parent.getAdapter().getItem(position);
			
			db2.execSQL("DELETE FROM step2 WHERE _id = "+ i2 + ";");
			
			arList2.clear();
			dbHelper2.close();
			setAdapterAndList22();
			break;
		case 3:
			DBHelper dbHelper3= new DBHelper(DBListAcitiviryStep2.this);
			SQLiteDatabase db3 = dbHelper3.getWritableDatabase();
			
			int i3 = arList3.get(position).id2;
			//int i = (Integer) parent.getAdapter().getItem(position);
			
			db3.execSQL("DELETE FROM step3 WHERE _id = "+ i3 + ";");
			
			arList3.clear();
			dbHelper3.close();
			setAdapterAndList23();
			break;
		}
		
	};
	
	
	
	
}
